<?php

class Index extends My_Controller {
    
    public function __construct() {
        parent::__construct();
        
        //load database connection
        $this->load->database();
        
        //load model
        $this->load->smc_model('category_model');
    }

    public function index() {
        
        $data['category'] = $this->get_category();
        
        //header module;
        $this->template->write_view('navMenu','themes/default/block/header.php');
        
        //parser data to navigation view
        $this->template->write_view('navMenu','themes/default/block/navMenu.php',$data);
        
        //mainContent module;
        $this->template->write_view('navMenu','themes/default/block/mainContent.php');
        
        //footer module;
        $this->template->write_view('navMenu','themes/default/block/footer.php');
        
        // Render template
        $this->template->render();
    }
        
    public function get_category(){
        
        //data category
        $dataReturn = "";
        
        //building data for navigation{get parent category}.
        $parent_query = Category_model::query()->where('category_parent_id', '0')->get();
        $all_cat_parent = $parent_query->result();
        
        if(!empty($all_cat_parent)){
            //foreach parent to get sub category
            foreach($all_cat_parent as $parent){
                $sub_query = Category_model::query()->where('category_parent_id', $parent->category_id)->get();
                $all_cat_sub = $sub_query->result();
                $dataReturn .= "<li><a href='#'><b>".$parent->name."</b></a><div><ul>";
                
                if(!empty($all_cat_sub)){
                    foreach($all_cat_sub as $sub){
                        $dataReturn .= "<li><a href='#url'>".$sub->name."</a></li>";
                    }
                }
                $dataReturn .= "<div style='clear:both'></div></ul></div>";
            }
        }
        return $dataReturn;
    }
    
    
}